﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.Serialization;
using UnityEngine.UI;

public class NormalSceneLoadingAnim : ViewControllerLoadAnimation
{
    //Animator m_Animator;
    GraphicRaycaster m_GraphicRaycaster;
    Canvas m_Canvas;
    
    ZUIAnimTools _mZuiAnimTools;   
    
    public int canvasSortingOrder = 100;
    public UnityEngine.GameObject trans;
    public float showAnimTime = 0.2f;
    public float loadingAnimTime = 0.2f;
    public float hideAnimTime = 0.4f;
    public AnimationCurve showAnimCurve = AnimationCurve.EaseInOut(0,0,1,1);
     public AnimationCurve hideAnimCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);

    public bool playSound;

    void  Awake()
    {
        //m_Animator = GetComponentInChildren<Animator>();
        m_GraphicRaycaster = GetComponent<GraphicRaycaster>();
        m_Canvas = GetComponent<Canvas>();

        _mZuiAnimTools = GetComponent<ZUIAnimTools>();
        if(_mZuiAnimTools == null) _mZuiAnimTools = gameObject.AddComponent<ZUIAnimTools>();
        
        if (trans)
            trans.gameObject.SetActive(false);
    }


    protected override  void OnDestroy()
    {
        m_OnInLoadingAnimEnd = null;
    }


    public bool showText = false;
    protected  override  void _PlayShowAnim(ViewController dc,Action onAnimEnd)
    {
        if (trans)
            trans.gameObject.SetActive(true);

        PlayAnim(() =>
        {

            if (onAnimEnd != null)
                onAnimEnd();
        });
        
        GameDebug.LogError($"_PlayShowAnim");

        if(playSound)
            AudioController.instance.PlayUISound(Audio.scene_load);
    }

    public SliderAnim sliderAnim;
    public Text inloadingText;
    public bool SelfPlaySliderAnim = true;
    Action m_OnInLoadingAnimEnd;
    protected override void _PlayInLoadingAnim( Action onEnd)
    {        
        m_OnInLoadingAnimEnd = onEnd;

        if (sliderAnim)
        {
            sliderAnim.gameObject.SetActive(true);

           // Invoke("OnLoadingAnimEnd",inLoadingTime);
            
            if (SelfPlaySliderAnim)
                sliderAnim.PlaySlider(this.loadingAnimTime,m_OnInLoadingAnimEnd);
        }
        else
        {
            PlayInLoading(onEnd);
        }
    }

    protected override void _PlayHideAnim(Action onAnimEnd)
    {
        Hide(() =>
        {
            if (trans)
                trans.gameObject.SetActive(false);

            if(sliderAnim)
                sliderAnim.ResetAnim();

            if (onAnimEnd != null)
                onAnimEnd();
        });
    }



    /// <summary>
    /// 播放切换场景过渡动画
    /// </summary>
    /// <param name="onFinished"></param>
    public void PlayAnim(Action onFinished)
    {
        //if (isFadingToBlack)
        //    return;

        GameDebug.Log("PlayAnim!!");
        
        m_Canvas.sortingOrder = canvasSortingOrder;
        
        if (m_GraphicRaycaster == null) m_GraphicRaycaster = GetComponent<GraphicRaycaster>();
        m_GraphicRaycaster.enabled = true;

        _mZuiAnimTools.InitCurve(showAnimCurve);
        _mZuiAnimTools.SetImgTxtAlpha(transform.GetComponent<RectTransform>(), 0);
        _mZuiAnimTools.UIAlpha(transform.GetComponent<RectTransform>(),1, showAnimTime, onFinished, ZUIAnimTools.EaseType.Curve,
            (color,img)=>
            {
                var sheetAnim = img.GetComponent<ImageSheetAnim>();
                if (sheetAnim)
                    sheetAnim.renderMat.color = color;

            });
        
        SetBlock(true);
    }


    private Action m_InLoadingEnd;
    void PlayInLoading(Action onEnd)
    {
       m_InLoadingEnd = onEnd;
       Invoke("OnInLoadingEnd",loadingAnimTime);
    }

    void OnInLoadingEnd()
    {
        if (m_InLoadingEnd != null)
            m_InLoadingEnd();
    }
    
    public void Hide(Action onFinished)
    {
        //Debug.Log("Hide!!");
        //if (isFadingToBright)
        //    return;

        m_Canvas.sortingOrder = canvasSortingOrder;
        
        if (m_GraphicRaycaster == null) m_GraphicRaycaster = GetComponent<GraphicRaycaster>();
        m_GraphicRaycaster.enabled = false;

        _mZuiAnimTools.InitCurve(hideAnimCurve);
        _mZuiAnimTools.UIAlpha(transform.GetComponent<RectTransform>(), 0, hideAnimTime, onFinished, ZUIAnimTools.EaseType.Curve,
            (color, img)=>
            {
            var sheetAnim = img.GetComponent<ImageSheetAnim>();
            if (sheetAnim)
                sheetAnim.renderMat.color = color;

        });
        
        SetBlock(false);
    }

    void SetBlock(bool block)
    {
        foreach (var img in GetComponentsInChildren<Image>())
        {
            img.raycastTarget = block;
        }
        foreach (var Text in GetComponentsInChildren<Text>())
        {
            Text.raycastTarget = block;
        }
    }
    
   

    float dataInitPercent = 0.4f;//数据加载的占比
    public void OnDataProgress(float progress)
    {
        GameDebug.Log("OnDataProgress:" + progress);

        if (sliderAnim)
            sliderAnim.SetSliderValue(dataInitPercent * progress);

        ///数据加载完，播放剩余进度
        if (progress >= 1 && sliderAnim != null)
            sliderAnim.PlaySlider(this.loadingAnimTime,m_OnInLoadingAnimEnd);
        
    }
    
    float timer = 0;
    string[] m_PointStrs = new string[4] { "",".", "..", "..." };
    int m_PointIndex = 0;
    public int LoadingTextAnimFPS = 6;
    float LoadingTextAnimTimePerFrame => LoadingTextAnimFPS == 0 ? 10000 : 1f / LoadingTextAnimFPS;

    void Update()
    {
        if (inLoadingAnim)
        {
            if(this.inloadingText)
            {
                timer += Time.deltaTime;
                if(timer > LoadingTextAnimTimePerFrame)
                {
                    timer = 0;

                    m_PointIndex ++;
                    m_PointIndex %= m_PointStrs.Length;

                    //Debug.Log("m_PointIndex:" + m_PointIndex);

                    inloadingText.text =  "Loading" + m_PointStrs[m_PointIndex];
                }
            }
        }
        
    }

    

}
